package com.hotel.controller;



import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hotel.entity.Dept;
import com.hotel.entity.IdType;
import com.hotel.entity.Position;
import com.hotel.entity.User;
import com.hotel.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	/**
	 * 登录
	 * @param request
	 * @return
	 */
	@RequestMapping(value="login",produces="application/jsp;charset=utf-8")
	public ModelAndView login(HttpServletRequest request,@Valid @ModelAttribute("user")User loUser,String isRememberMe){
		ModelAndView view = new ModelAndView();
		Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token=new UsernamePasswordToken(loUser.getEmail(), loUser.getPassword());
		try{
			//是否需要记住密码
			if(isRememberMe!=null){
				//是否已经记住密码
				if(!subject.isRemembered()){
					//记住密码
					token.setRememberMe(true);
				}
			}
			subject.login(token);
			view.setViewName("home");
		}catch(Exception e){
			request.setAttribute("user", loUser);
			request.setAttribute("errorMsg", "邮箱或密码错误!");
			if(subject.getPrincipal()!=null){
				view.setViewName("home");
				return view;
			}
			view.setViewName("login");
		}
		return view;
	}
	
	
	/**
	 * 查询全部信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="userList")
	@ResponseBody
	public ModelAndView findAllUser(HttpServletRequest request,@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		ModelAndView view = new ModelAndView();
		List<User> users = userService.findAllUser(pageNo,pageSize);
		view.addObject("users",users);
		view.addObject("pageNo",pageNo);
		view.addObject("pageSize",pageSize);
		view.setViewName("views/user/userList");
		return view;
	}
	/**
	 * 查询邮箱是否存在
	 * @param email	邮箱
	 * @return	yes/no
	 */
	@RequestMapping(value="findEmailExists",method = RequestMethod.POST)
	@ResponseBody
	public User findEmailIsExists(String email){
		User u = userService.getByUserName(email);
		return u;
	}
	/**
	 * 查询全部证件类型
	 * @return
	 */
	@RequestMapping(value="idTypeList",method = RequestMethod.POST)
	@ResponseBody
	public List<IdType> findAllIdType(){
		return userService.findAllIdType();
	}
	/**
	 * 查询全部部门
	 * @return
	 */
	@RequestMapping(value="deptList",method = RequestMethod.POST)
	@ResponseBody
	public List<Dept> findAllDept(){
		return userService.findAllDept();
	}
	/**
	 * 按部门ID查询职位
	 * @return
	 */
	@RequestMapping(value="positionList",method = RequestMethod.POST)
	@ResponseBody
	public List<Position> findPositionByDeptId(Integer deptId){
		return userService.findPositionByDeptId(deptId);
	}
	/**
	 * 按职位ID查询部门
	 * @param positionId
	 * @return
	 */
	@RequestMapping(value="deptByPid",method = RequestMethod.POST)
	@ResponseBody
	public Dept findDeptByPositionId(Integer positionId){
		return userService.findDeptByPositionId(positionId);
	}
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="insertUser",method = RequestMethod.POST)
	public String insertUser(User user){
		userService.insert(user);
		return "forward:userList.action";
	}
	
	/**
	 * 更新用户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="updateUser",method = RequestMethod.POST)
	public String updateUser(HttpServletRequest request,User user){
		userService.updateByPrimaryKey(user);
		findAllUser(request,1,10);
		return "forward:userList.action";
	}
	
	/**
	 * 注销/切换用户
	 * @param request
	 * @return
	 */
	@RequestMapping("logout")  
    public String logout(HttpServletRequest request){  
        SecurityUtils.getSubject().logout();    
        return "login";  
    }  
	/**
	 * 用户已认证返回home,用户未认证返回login
	 * @param request
	 * @return home/login
	 */
	@RequestMapping("home")  
	public String home(HttpServletRequest request){
		 return SecurityUtils.getSubject().isAuthenticated() ? "home" : "login";  
	}
}
